package com.heima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.heima.common.PageResult;
import com.heima.mapper.SkuClassMapper;
import com.heima.pojo.SkuClass;
import com.heima.pojo.dto.SkuClassPageQueryDTO;
import com.heima.service.SkuClassService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class SkuClassServiceImpl implements SkuClassService {

    @Autowired
    private SkuClassMapper skuClassMapper;


    @Override
    public Boolean add(SkuClass skuClass) {
        int result = skuClassMapper.insert(skuClass);
        return result != 0;
    }

    @Override
    public PageResult skuQuery(SkuClassPageQueryDTO dto) {
        dto.check();
        PageHelper.startPage(dto.getPageIndex(),dto.getPageSize());
        Page<SkuClass> page = skuClassMapper.skuQuery(dto);
        return new PageResult(dto.getPageIndex(), dto.getPageSize(), page.getPages(), page.getTotal(),page.getResult());
    }

    @Override
    public SkuClass findById(String  classId) {
        return skuClassMapper.selectById(classId);
    }

    @Override
    public Boolean update(String classId, SkuClass skuClass) {
        skuClass.setClassId(Integer.parseInt(classId));
        int result = skuClassMapper.updateById(skuClass);
        return result == 1;
    }

    @Override
    public void delete(String classId) {
        skuClassMapper.deleteById(classId);
    }

}
